<%@page import="net.groupme.group.GroupListsManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Group"%>
<%@page import="java.util.Iterator"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="java.util.Collection"%>
<%@page import="net.groupme.model.Tag"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.user.UserManager"%>
<%@page import="net.groupme.model.User"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.lang.StringEscapeUtils"%>
<% GroupListsManager glm = new GroupListsManager(); %>
<%@page import="net.groupme.search.SEARCH_CONFIGURATION"%>
<%@page import="net.groupme.search.utils.SearchUtility"%>
<div id="mainGroupwindowContent">
	<% 		
	 		List<Group> groupList = (List<Group>) request.getAttribute(VIEW_VOCABULARY.SEARCH_RESULT_LIST);
			if(groupList == null){
				groupList = SearchUtility.getLatestGroups(0, SEARCH_CONFIGURATION.NUMBER_OF_GROUPS_TO_BE_LISTED, SEARCH_CONFIGURATION.NUMBER_OF_MINIMUM_RESOURCES_IN_GROUPS_FOR_STARTPAGE);
			}
			Iterator<Group> groupIterator = groupList.iterator(); %>
			<%if(request.getAttribute(VIEW_VOCABULARY.PARAMETER_POPULAR) != null && VIEW_VOCABULARY.GROUPS.equals(request.getAttribute(VIEW_VOCABULARY.PARAMETER_POPULAR).toString())){%>
				<h4>Popular groups:</h4>
			<%} else if(request.getAttribute(VIEW_VOCABULARY.PARAMETER_RANDOM) != null && VIEW_VOCABULARY.GROUPS.equals(request.getAttribute(VIEW_VOCABULARY.PARAMETER_RANDOM).toString())){%>
				<h4>Randomly selected groups:</h4>
			<%} else if(request.getAttribute(VIEW_VOCABULARY.PARAMETER_LATEST) != null && VIEW_VOCABULARY.GROUPS.equals(request.getAttribute(VIEW_VOCABULARY.PARAMETER_LATEST).toString())){%>
				<h4>Latest groups:</h4>
			<%} else { %>
				<h4>Latest groups:</h4>
			<%} %>
			<%		String tmpKeywordDelimeter = "";
		String keyword = null;
		while (groupIterator.hasNext()){
			keyword = null;
			Group actualGroup = groupIterator.next(); 
			if(!MODEL_CONSTANTS.RESOURCE_TYPE_QUEUE.equals(actualGroup.getType())){%>

			<%
			Iterator<User> iter = actualGroup.getUsers("owner").iterator();
			User currentGroupUser = null;
			if (iter.hasNext()){
				currentGroupUser = iter.next();
			}
			String thumbnail = null;
			
			try {
				thumbnail = actualGroup.getActualGroupVisualization(currentGroupUser.getId()).getThumbnail();
			}
			catch(Exception e){
				
			}
			
			if (thumbnail==null){
				thumbnail = actualGroup.getURLOfAPictureInGroup();
			}
			
			if (thumbnail==null & actualGroup.getURLOfAPictureInGroup()==null){
			thumbnail = request.getContextPath() + "/images/groupimg-default.gif";
			}
			
			String groupImgError = request.getContextPath() + "/images/groupimg-default.gif";
			%>
			<div class="groupWithinGroupList">
			<a href="<%= actualGroup.getURI() %>"><img style="float:left; margin-right:1em; border: 0pt none" src="<%= thumbnail %>" width="120" height="80" alt="Group"  onerror="this.src='<%=groupImgError %>';"/></a>
			<strong>
				<a href="<%= actualGroup.getURI() %>"><%= actualGroup.getName() %></a>
			</strong>
			<% //TODO: HACK um NullPointer zu entgehen: (HACK wird eigentlich nicht mehr benoetigt)
				String dateCreated = actualGroup.getDateCreated();
			if(dateCreated == null){
				dateCreated = "2008-11-06";
			}else{
				dateCreated = actualGroup.getDateCreated().substring(0,11);
			}
			System.out.println("cu " + actualGroup.getId() + "cu: " +currentGroupUser);
			%>
			<p> <span style="font-size:0.8em;color:gray">Created: <%= dateCreated %></span> <span style="font-size:0.8em;color:gray">by <a style="color:gray" href="<%= VIEW_VOCABULARY.RESTful_USER_URI_PATTERN + currentGroupUser.getUserName() %>"> <%= currentGroupUser.getUserName() %></a></span></p>
			<p> <span style="font-size:0.8em;color:black">
			<% if (actualGroup.getDescription() != null && actualGroup.getDescription().length()>70) {%>
				<%= actualGroup.getDescription().substring(0, 70)+"..."  %>
			<% }else if(actualGroup.getDescription() != null){ %>
				<%= actualGroup.getDescription()%>
			<% }else{%>
				<%= "Group about '" + actualGroup.getName() + "'"%>
			<% } %>
			</span></p>
			<% //List tags:
			Collection<Tag> tags = actualGroup.getTags(); 
			String htmlTags = "";
			Iterator<Tag> tagIterator = tags.iterator();
			if(tagIterator.hasNext()){%>
				<small><strong>Tags:</strong>
			<%
			}else {
				htmlTags="<small><strong>Tags:</strong> No tags</small>";
			}
			
			
			while(tagIterator.hasNext()){
				Tag tag = tagIterator.next();
				if(tag.getKeyword().contains(" ")){
					tmpKeywordDelimeter = "%22";
				}else{
					tmpKeywordDelimeter = "";
				}
				keyword = StringEscapeUtils.escapeHtml(tag.getKeyword());
				htmlTags+= "<a style=\"font-size: small\" href=\"" + tag.getURI() + "\" "+  " title=\"Search for groups and resources related with " + keyword + "\"> "  + keyword + "</a>";
				if(tagIterator.hasNext()){
					htmlTags+= ", ";
				}
			
				%>
				
			
			</small>
			<%}  %>
				<%=htmlTags%>
			</div>
			<br style="clear:left"/>
		<%	} //end of "if queue"
		}//end of while 
		%>
	
	</div>
	
	<%
	if(request.getAttribute(VIEW_VOCABULARY.PARAMETER_LATEST) != null && VIEW_VOCABULARY.GROUPS.equals(request.getAttribute(VIEW_VOCABULARY.PARAMETER_LATEST).toString())){
		int currentPage = 0;
		if(request.getParameter("pageID") != null){
			currentPage = Integer.parseInt(request.getParameter("pageID"));
		}else {
			currentPage = 1;
		}
	
		int resultListSize = 0;
		if(request.getAttribute("numberOfLatestGroups") != null){
			resultListSize = Integer.parseInt(request.getAttribute("numberOfLatestGroups").toString());
		}else {
		
		}
		String pageIdURL = VIEW_VOCABULARY.RESTful_LATEST_GROUPS_URI_PATTERN;
	%>
	<%@ include file="listOfPageNumbers.jsp" %>
	<%
	}
	else if(request.getAttribute(VIEW_VOCABULARY.PARAMETER_RANDOM) == null && request.getAttribute(VIEW_VOCABULARY.PARAMETER_POPULAR) == null){
		%><div style="text-align:center;"><a href="<%= VIEW_VOCABULARY.RESTful_LATEST_GROUPS_URI_PATTERN %>?pageID=2">more...</a></div><%
		}
	%>